<template>
  <!-- 评教管理 -->
  <div>
    <div class="edu-page-warp">
      <condition-card @reset="reset" @search="searchList" :label-width="labelWidth" style="margin-bottom: 20px;">
        <el-row :gutter="20">
          <!-- 开课学院 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.openClassCollege')">
              <DictSelect
                labelAll="请选择"
                :disabled="facultyDisable"
                filterable multiple collapse-tags
                v-model="queryParams.facultyList"
                :data="options.faculty"
                @change="handleChangeFaculty"
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 开课学院 -->
          <!-- <el-col :span="8">
            <el-form-item :label="$t('label.openClassCollege')">
              <el-select
                v-model="queryParams.facultyList"
                :disabled="ifDisabled"
                @change="changeFaculty"
                clearable
                filterable
                multiple
                collapse-tags
                style="width: 100%;">
                <el-option
                  v-for="item in facultyList"
                  :key="item.deptCode"
                  :label="item.deptName"
                  :value="item.deptCode">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <!-- 考核方式 -->
          <el-col :span="8">
            <el-form-item :label="$t('label.assessmentMode')">
              <DictSelect
                v-model="queryParams.assessmentModeList"
                :data="options.assessmentMode"
                clearable
                filterable
                multiple
                collapse-tags>
              </DictSelect>
            </el-form-item>
          </el-col>
          <!-- 课程性质 -->
          <el-col :span="8">
            <el-form-item :label="$t('courseSummary.nature')" prop="courseCategory">
              <dict-select
                v-model="queryParams.natureList"
                :data="options.courseNatureList"
                :show-all="false"
                clearable
                filterable
                multiple
                collapse-tags
              />
            </el-form-item>
          </el-col>
          <!-- 授课语言 -->
          <el-col :span="8">
            <el-form-item :label="$t('授课语言')">
              <el-select v-model="queryParams.teachingLanguageList"
                clearable filterable
                multiple collapse-tags style="width: 100%;">
                <el-option
									v-for="item in options.teachingLanguage"
									:key="item.value"
									:label="item.label"
									:value="item.value"
								></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 课程名称 -->
          <el-col :span="8">
            <el-form-item :label="$t('课程名称')">
              <el-input v-model="queryParams.courseName" clearable :placeholder="$t('请输入')"></el-input>
            </el-form-item>
          </el-col>
          <!-- 课程层次 -->
          <el-col :span="8">
            <el-form-item  :label="$t('课程层次')">
              <DictSelect
                  v-model="queryParams.trainingLevelList"
                  :data="options.stuTrainingLevel"
                  clearable
                  filterable
                  multiple
                  collapse-tags
              ></DictSelect>
            </el-form-item>
          </el-col>
          <!-- 课程代码 -->
          <el-col :span="8">
            <el-form-item :label="$t('课程代码')">
              <el-input v-model="queryParams.courseCode" :placeholder="$t('请输入')"></el-input>
            </el-form-item>
          </el-col>
          <!-- 授课教师区分 -->
          <el-col :span="8" v-if="ifShowTeacherParam">
            <el-form-item :label="$t('授课教师区分')">
              <el-select v-model="queryParams.firstTeacherStatus" clearable style="width: 100%;">
                <el-option
                  v-for="item in teacherTypeList"
                  :key="item.code"
                  :label="item.teacherTypeName"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- 工号、姓名 -->
          <el-col :span="8">
            <el-form-item :label="$t('工号/姓名')">
              <el-input v-model="queryParams.teacherCodeOrName" maxlength="32" :placeholder="$t('请输入')"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>

      <!-- 表格card -->
      <el-card shadow="never">
        <div class="edu-opertion-bar">
          <div class="float-left edu-page-title">{{title}}</div>
          <div class="float-right">
            <!-- <el-button type="primary" @click="calculateScore()">{{$t('计算分数')}}</el-button> -->
            <el-button type="primary" @click="statistics()">统计</el-button>
            <el-button v-if="ifRelease" type="primary" @click="exportDetail()">{{$t('导出明细')}}</el-button>
            <el-button v-if="ifRelease" type="primary" @click="exportData()">{{$t('message.export')}}</el-button>
          </div>
        </div>
        <!-- 主页表格 -->
        <edu-table :url="url" ref="mainTable" :order="true" :selection="true">
          <edu-table-column :label="$t('开课学院')" prop="facultyName"></edu-table-column>
          <edu-table-column :label="$t('课程性质')" prop="natureName"></edu-table-column>
          <edu-table-column :label="$t('考核方式')" prop="assessmentModeName"></edu-table-column>
          <edu-table-column :label="$t('授课语言')" prop="teachingLanguageName"></edu-table-column>
          <edu-table-column :label="$t('课程层次')" prop="trainingLevelName"></edu-table-column>
          <edu-table-column :label="$t('课程名称')" prop="courseName"></edu-table-column>
          <edu-table-column :label="$t('课程代码')" prop="courseCode"></edu-table-column>
          <edu-table-column :label="$t('教学班代码')" prop="teachingClassId"></edu-table-column>
          <edu-table-column :label="$t('第一任课教师')" prop="firstTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('其他任课教师')" prop="otherTeacherNameCode"></edu-table-column>
          <edu-table-column :label="$t('班级人数')" prop="studentNumber"></edu-table-column>
          <edu-table-column :label="$t('评价完成情况')" min-width="120px" v-if="teacherComeIn">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.finishState === '1'">已完成</el-tag>
              <el-tag type="danger" v-if="scope.row.finishState === '0'">未完成</el-tag>
              <el-tag type="info" v-if="scope.row.finishState === '2'">已过期</el-tag>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('未评人数')" v-if="ifShowNotFillPeople && ifRelease">
            <template slot-scope="scope">
              <el-button type="text" @click="notFillInPeopleClick(scope.row)">{{scope.row.studentNoNumber}}</el-button>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('操作(平均分)')" v-if="ifRelease">
            <template slot-scope="scope">
              <el-button type="text" @click="checkClick(scope.row)">{{`${$t('查看')}(${scope.row.averageScore})`}}</el-button>
            </template>
          </edu-table-column>
        </edu-table>
        <edu-export-dialog :disabled="exportButton" :title="$t('message.prompt')" ref="downloadRef" :data="exportResult"></edu-export-dialog>
        <el-dialog
          title="未评人数名单详情"
          :visible.sync="notFillDialog"
           >
          <table class="classTable" cellpadding="0" cellspacing="0">
            <tr style="background: #EEF7FF;">
              <td colspan="5">姓名/学号</td>
            </tr>
            <tr v-for="(item, index) in stuNotFillList" :key="index">
              <td v-for="(item1, index1) in item" :key="index1">
                {{item1}}
              </td>
            </tr>
          </table>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils"
import baseUrl from 'common/src/api/teachingEvaluation/env'
import { queryDic } from "common/src/api/dictionary"
// import innovativeApi from 'common/src/api/innovative/innovative'
import courseEvaluationApi from 'common/src/api/teachingEvaluation/courseEvaluation'
import questionnaireApi from 'common/src/api/teachingEvaluation/questionnaire'
import templateApi from 'common/src/api/teachingEvaluation/template'
export default {
  props: {
    ifShowNotFillPeople: {
      type: Boolean,
      default: true
    },
    ifShowTeacherParam: {
      type: Boolean,
      default: true
    },
    teacherComeIn: {
      type: Boolean,
      default: false
    },
    // 是否已发布
    ifRelease: {
      type: Boolean,
      default: true
    },
    pageFrom: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      routeParams: {},
      title: '问卷主题列表',
      url: '',
      labelWidth: '120px',
      exportButton: false,
      exportResult: null,
      notFillDialog: false,
      currentCalendarId: '',
      queryParams: {
        calendarId: '',
        facultyList: [],
        assessmentModeList: [],
        natureList: [],
        teachingLanguageList: [],
        courseName: '',
        trainingLevelList: [],
        courseCode: '',
        firstTeacherStatus: '',
        teacherCodeOrName: ''
      },
      options: {
        courseNatureList: [], //课程性质
        trainingLevelList: [], //培养层次
        assessmentMode: [], // 考核方式
        stuTrainingLevel: [], //课程层次
        teachingLanguage: [], //授课语言
        faculty: [] //学院
      },
      teacherTypeList: [
        {
          teacherTypeName: '第一任课教师',
          code: '1'
        },
        {
          teacherTypeName: '其他任课教师',
          code: '0'
        }
      ],
      tableData: [],
      facultyList: [],
      dataList: [],
      stuNotFillList: [],
      checkData: {},
      facultyDisable: false
    }
  },
  watch: {
    teacherComeIn(value) {
      if (value) {
        this.url = `${baseUrl}/teacherManagement/object/look/getPageList`
      } else {
        this.url = `${baseUrl}/studentManagement/object/look/getPageList`
      }
    },
    '$route'(to, from) {
        if (Object.values(to.query).length > 0) {
          this.routeParams = _.cloneDeep(to.query)
          if (this.routeParams.managementId) {
            this.queryParams.managementId = to.query.managementId;
            this.queryParams.calendarId = to.query.calendarId;
            this.searchList()
          }
        }
    }
  },
  created() {
    if (!this.teacherComeIn) {
      this.url = `${baseUrl}/studentManagement/object/look/getPageList`
    } else {
      this.url = `${baseUrl}/teacherManagement/object/look/getPageList`
    }
    if (sessionStorage.getItem('currFacultyCode')) {
      this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      this.facultyDisable = true
    }
    this.getOptions();
  },
  mounted() {
    if (this.$route.query.managementId) {
      this.queryParams.managementId = this.$route.query.managementId;
      this.queryParams.calendarId = this.$route.query.calendarId;
      this.searchList()
    }
  },
  methods: {
    // 查询
    searchList() {
      this.$refs.mainTable.pageNum = 1;
      this.queryParams.courseCode = this.queryParams.courseCode.trim()
      this.queryParams.courseName = this.queryParams.courseName.trim()
      this.queryParams.teacherCodeOrName = this.queryParams.teacherCodeOrName.trim()
      this.$refs.mainTable.search(this.queryParams)
    },
    // 统计
    statistics() {
        let parm = {
          objectConfigIdList: [],
          managementId: this.$route.query.managementId ? this.$route.query.managementId : this.routeParams.managementId
        }
        if(this.$refs.mainTable.getCheckData()) {
          this.$refs.mainTable.getCheckData().forEach(val=> {
          parm.objectConfigIdList.push(val.id)
        })
      }
      if(this.pageFrom === 'evaTeach') {
        parm.facultyList = this.queryParams.facultyList
        parm.assessmentModeList = this.queryParams.assessmentModeList
        parm.natureList = this.queryParams.natureList
        parm.teachingLanguageList = this.queryParams.teachingLanguageList
        parm.courseName = this.queryParams.courseName
        parm.trainingLevelList = this.queryParams.trainingLevelList
        parm.courseCode = this.queryParams.courseCode
        parm.firstTeacherStatus = this.queryParams.firstTeacherStatus
        parm.teacherCodeOrName = this.queryParams.teacherCodeOrName
        templateApi.statistics(parm).then(res=> {
          this.checkData = res.data
          this.$emit('goToCheck', this.checkData,this.pageFrom)
        })
      } else if(this.pageFrom === 'evaLearn') {
        templateApi.questionnaireTeacher(parm).then(res=> {
          this.checkData = res.data
          this.$emit('goToCheck', this.checkData,this.pageFrom)
        })
      }
    },
    // 重置查询参数
    reset() {
      this.queryParams = {
        managementId: this.$route.query.managementId ? this.$route.query.managementId : this.routeParams.managementId,
        calendarId: this.$route.query.calendarId ? this.$route.query.calendarId : this.routeParams.calendarId,
        facultyList: [],
        assessmentModeList: [],
        natureList: [],
        teachingLanguageList: [],
        courseName: '',
        trainingLevelList: [],
        courseCode: '',
        firstTeacherStatus: '',
        teacherCodeOrName: ''
      };
      if (this.facultyDisable) {
        this.queryParams.facultyList.push(sessionStorage.getItem('currFacultyCode'))
      }
      this.searchList();
    },
    // 获取学院数据
    // getFaculty() {
    //   innovativeApi.getAllFaculty().then(res => {
    //     this.facultyList = res.data;
    //   })
    // },
    // 未评人数
    notFillInPeopleClick(row) {
      this.notFillDialog = true
      courseEvaluationApi.getStuNotFillInList({id: row.id}).then(res => {
        if (res.data && res.data.length > 0) {
          console.log('res.data', res.data)
          this.stuNotFillList = res.data
        } else {
          this.stuNotFillList = []
        }
      })
    },
    // 获取字典数据
    async getOptions() {
      //筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["X_KCXZ", "X_PYCC", "X_KSLX", "X_PYCC", "X_SKYY", "X_YX"]
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            courseNatureList: "X_KCXZ", // 课程性质
            trainingLevelList: "X_PYCC", // 培养层次
            assessmentMode: "X_KSLX", // 考核方式
            stuTrainingLevel: "X_PYCC", // 课程层次
            teachingLanguage: "X_SKYY", // 授课语言
            faculty: "X_YX"
          },
          res.data,
          this.options
        );
      });
    },
     // 导出明细
    exportDetail() {
      if (this.$refs.mainTable.getData().length <= 0) {
        this.$message.error(this.$t('result.noExportData'));
        return;
      }
      const checkDatas = this.$refs.mainTable.getCheckData();
      let ids = checkDatas.map(item => {
        return item.id
      })
      let param = JSON.parse(JSON.stringify(this.queryParams))
      param.idList = ids
      this.exportResult = null;
      this.$refs.downloadRef.open();
      this.exportButton = true;
      // 评教导出
      if (this.ifShowTeacherParam) {
        courseEvaluationApi.exportStuThemeDetailList(param).then(res =>{
          if (res.code == 200) {
            this.delayRequest(res.data.key);
          } else {
            this.$refs.downloadRef.close();
            this.exportButton = false;
          }
        })
      } else {
        courseEvaluationApi.exportTeacherThemeDetailList(param).then(res =>{
          if (res.code == 200) {
            this.delayRequest(res.data.key);
          } else {
            this.$refs.downloadRef.close();
            this.exportButton = false;
          }
        })
      }
    },
     // 导出
    exportData() {
      if (this.$refs.mainTable.getData().length <= 0) {
        this.$message.error(this.$t('result.noExportData'));
        return;
      }
      const checkDatas = this.$refs.mainTable.getCheckData();
      let ids = checkDatas.map(item => {
        return item.id
      })
      let param = JSON.parse(JSON.stringify(this.queryParams))
      param.idList = ids
      this.exportResult = null;
      this.$refs.downloadRef.open();
      this.exportButton = true;
      if (this.ifShowTeacherParam) {
        courseEvaluationApi.exportStuThemeList(param).then(res =>{
          if (res.code == 200) {
            this.delayRequest(res.data.key);
          } else {
            this.$refs.downloadRef.close();
            this.exportButton = false;
          }
        })
      } else {
        courseEvaluationApi.exportTeacherThemeList(param).then(res =>{
          if (res.code == 200) {
            this.delayRequest(res.data.key);
          } else {
            this.$refs.downloadRef.close();
            this.exportButton = false;
          }
        })
      }
    },
    // 导出异步
    delayRequest(key) {
      setTimeout(() => {
        courseEvaluationApi.exportStatus(key).then(res => {
          this.exportResult = res.data;
          if (res.code == 200 && res.data.status) {
            this.exportButton = false;
            let path =
              `${baseUrl}/file/exportExcel?path=` +
              res.data.path;
            // window.open(path);
            window.location.href = path;
            this.$message.success(this.$t('result.exportSuccessfully'));
            this.$refs.downloadRef.close();
          } else {
            this.delayRequest(key);
          }
        });
      }, 3000);
    },
    // 查看
    checkClick(row) {
      // 评学
      if (this.teacherComeIn) {
        let param = {
          teacherCode: row.teacherCode,
          courseCode: row.courseCode,
          objectConfigId: row.id,
          managementId: row.managementId
        }
        questionnaireApi.getQuestionnaireResultTeacher(param).then(res => {
          if (res.code === 200) {
            this.checkData = res.data
            this.$emit('goToCheck', this.checkData)
          }
        })
        // 评教
      } else {
        let param = {
          objectConfigId: row.id,
          managementId: row.managementId
        }
        questionnaireApi.getQuestionnaireResultTeacherTeach(param).then(res => {
          if (res.code === 200) {
            this.checkData = res.data
            this.$emit('goToCheck', this.checkData)
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.classTable{
  &.noPoint td{
    cursor: default;
    width: auto;
  }
  border: solid 1px #9c9c9c;
  border-right: 0px;
  border-bottom: 0px;
  width: 100%;
  text-align: left;
  td{
    border: solid 1px #9c9c9c;
    border-top: 0px;
    border-left: 0px;
    padding: 8px;
    width: 100px;
    cursor: pointer;
    span {
      display: inline-block;
      overflow: hidden;
    }
  }
  td.bgGray {
    background: #e0e0e0;
  }
  td.seletedClass{
    background: yellow;
  }
  td.seletedClass.preSeletedClass{
    background: #b7b7f9;
  }
  td.preSeletedClass{
    background: #89ffde;
    // animation:myfirst 3s linear 1s infinite alternate;
  }
  td.disable {
    cursor: not-allowed;
  }
}
</style>
